/**
 * 
 */
package com.kebo2.client;

import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.dom.client.Style.Unit;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;

/**
 * @author Tim
 *
 */
public class UIBeforeLogin extends UserInterface {
	private Button helpButton;
	private Button browseButton;
	private Button statButton;
	private Button searchButton;
	private TextBox searchQuery;
	
	public UIBeforeLogin() {
		RootLayoutPanel rootPanel = RootLayoutPanel.get();
		rootPanel.setSize("100%", "100%");
		
		mainPanel = new DockLayoutPanel(Unit.EM);
		rootPanel.add(mainPanel);
		mainPanel.setSize("100%", "100%");
		
		dock = new HorizontalPanel();
		dock.setStyleName("dock");
		mainPanel.addNorth(dock, 6.3);
		dock.setSize("100%", "100px");
		
		helpButton = new Button("");
		helpButton.setTitle("Klik voor hulp");
		helpButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				new Help();
			}
		});
		helpButton.setStyleName("gwt-Button-helpButton");
		dock.add(helpButton);
		helpButton.setSize("48px", "48px");
		
		browseButton = new Button("");
		browseButton.setTitle("Klik om door de database te bladeren");
		browseButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				new Browser();
			}
		});
		browseButton.setStyleName("gwt-Button-browseButton");
		dock.add(browseButton);
		browseButton.setSize("48px", "48px");
		
		statButton = new Button("");
		statButton.setTitle("Klik om statistieken te bekijken");
		statButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				new Stats();
			}
		});
		statButton.setStyleName("gwt-Button-statButton");
		dock.add(statButton);
		statButton.setSize("48px", "48px");
		
		searchButton = new Button("");
		searchButton.setTitle("Klik om te zoeken");
		searchButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				String query = searchQuery.getValue();
				if(query.length() < 1)
					new UserInterface.notification("Vul een zoekwoord in.");
				else
					new Search(true, 0);
			}
		});
		searchButton.setStyleName("gwt-Button-search");
		dock.add(searchButton);
		searchButton.setSize("48px", "48px");
		
		searchQuery = new TextBox();
		searchQuery.addKeyDownHandler(new KeyDownHandler() {
			public void onKeyDown(KeyDownEvent event) {
				if(event.getNativeKeyCode() ==  KeyCodes.KEY_ENTER) {
					String query = searchQuery.getValue();
					if(query.length() < 1)
						new UserInterface.notification("Vul een zoekwoord in.");
					else
						Search.DirectSearch(query);
				}		
			}
		});
		searchQuery.setStyleName("gwt-TextBox-searchQuery");
		searchQuery.setText("Vul een zoekwoord in ");
		dock.add(searchQuery);
		searchQuery.setWidth("250px");
		
		systembar = new DockLayoutPanel(Unit.EM);
		systembar.setStyleName("systembar");
		mainPanel.addSouth(systembar, 2.5);
		systembar.setSize("100%", "30px");
		
		userButton = new Button("");
		userButton.setStyleName("gwt-Button-user");
		systembar.addEast(userButton, 3.4);
		userButton.setSize("32px", "32px");
		
		menu = new FlowPanel();
		menu.setStyleName("menu");
		mainPanel.addNorth(menu, 1.7);
		menu.setSize("100%", "23px");
		
		iconFlexTable = new FlexTable();
		menu.add(iconFlexTable);
		
		closeImg = new Image("img/close.png");
		closeImg.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				UserInterface.closeWindow();
				UserInterface.clearIcons();
			}
		});
		closeImg.setTitle("Sluit");
		closeImg.setStyleName("iconImg");
		iconFlexTable.setWidget(0, 0, closeImg);
		
		closeTxt = new HTML("Sluit");
		closeTxt.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				UserInterface.closeWindow();
				UserInterface.clearIcons();
			}
		});
		closeTxt.setStyleName("iconTxt");
		iconFlexTable.setWidget(0, 1, closeTxt);
		
		exportImg = new Image("img/export.png");
		exportImg.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				new Export();
			}
		});
		exportImg.setTitle("Exporteer");
		exportImg.setStyleName("iconImg");
		iconFlexTable.setWidget(0, 2, exportImg);
		
		exportTxt = new HTML("Exporteer");
		exportTxt.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				new Export();
			}
		});
		exportTxt.setStyleName("iconTxt");
		iconFlexTable.setWidget(0, 3, exportTxt);
		
		window = new DockLayoutPanel(Unit.EM);
		window.setVisible(false);
		window.setStyleName("window");
		mainPanel.add(window);
		
		new UserInterface.notification("Welkom.");
	}
}
